/**
 * 
 */
package bai7.com.server;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

/**
 * @author nglehien Oct 22, 2012 - 8:46:08 PM
 */
public class StringReverseRemote extends UnicastRemoteObject implements
		StringReverseInterface {

	private static final long serialVersionUID = 3262078956667931554L;
	private FileInputStream fileInputStream;
	private BufferedReader bufferedReader;
	public static final String REVERSE_STRING = "REVERSE_STRING";

	protected StringReverseRemote() throws RemoteException {
		super();
	}

	@Override
	public boolean openFile(String filename) throws RemoteException {
		try {
			File file = new File(filename);
			fileInputStream = new FileInputStream(file);
			bufferedReader = new BufferedReader(new InputStreamReader(
					fileInputStream, "UTF-8"));
			return true;
		} catch (UnsupportedEncodingException | FileNotFoundException e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public String nextLine() throws IOException {
		return bufferedReader.readLine();
	}

	@Override
	public boolean closeFile() throws RemoteException {
		try {
			fileInputStream.close();
			bufferedReader.close();
			return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public String replace(String s, String s1, String s2)
			throws RemoteException {
		return s.replaceAll(s1, s2);
	}

}
